package home.control;

import home.control.data.FactoryData;
import home.control.data.IGetData;
import home.control.logicElements.Action;
import home.control.logicElements.ActionTypes;
import home.control.logicElements.HomeParts;
import home.control.logicElements.Plan;
import home.control.service.GetData;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class Scheduler extends Activity implements OnItemSelectedListener, OnItemLongClickListener, OnClickListener {

	private Spinner selectedPlanSP;
	private ListView planLV;
	private ArrayAdapter<Plan> spinnerAdapter;
	private ArrayAdapter<Action> listAdapter;
	private AlertDialog addDialog;
	private AlertDialog addPlanDialog;
	private String TAG = "Scheduler";
	private Spinner homePart;
	private Spinner actionChose;
	private TimePicker timePicker;
	private Button isActiveBT;
	private ArrayAdapter<ActionTypes> actionTypesSPA;
	private int selectedPos;
	private IGetData dataConnect;
	private EditText newPlanName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scheduler);

		dataConnect =  GetData.getInstance();

		selectedPlanSP = (Spinner) findViewById(R.id.selectPlanSP);
		spinnerAdapter = new ArrayAdapter<Plan>(this, android.R.layout.simple_spinner_item);
		selectedPlanSP.setAdapter(spinnerAdapter);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		isActiveBT = (Button) findViewById(R.id.isActivePlanBT);
		isActiveBT.setOnClickListener(this);
		updatePlans();
		selectedPlanSP.setOnItemSelectedListener(this);

		planLV = (ListView) findViewById(R.id.planLV);
		listAdapter = new ArrayAdapter<Action>(this, android.R.layout.simple_list_item_1);
		planLV.setAdapter(listAdapter);
		planLV.setOnItemLongClickListener(this);
		
		
	}

	private void updatePlans() {
		spinnerAdapter.clear();
		List<Plan> plans = dataConnect.getAllPlans();
		for (Plan p : plans) {
			spinnerAdapter.add(p);
		}
		spinnerAdapter.notifyDataSetChanged();
	}

	private void updateActionList() {
		Plan selectedItem = (Plan) selectedPlanSP.getSelectedItem();
		if(selectedItem == null){
			selectedItem = spinnerAdapter.getItem(0);
		}
		listAdapter.clear();
		List<Action> actions = selectedItem.getActions();
		Collections.sort(actions);
		for (Action a : actions) {
			listAdapter.add(a);
		}
		listAdapter.notifyDataSetChanged();
	}

	private void updateCanChangeToActive() {
		updatePlans();
		Plan p = (Plan) selectedPlanSP.getSelectedItem();
		if (p == null) {
			Log.d(TAG, "Ma wartość null");
			return;
		}
		if (p.isActive()) {
			isActiveBT.setText("Aktywny");
			isActiveBT.setEnabled(false);
		}
		else {
			isActiveBT.setText("Aktywuj");
			isActiveBT.setEnabled(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addActiomMI: {
			AlertDialog.Builder builder;

			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.addactivitylayout, (ViewGroup) findViewById(R.id.layout_root));

			homePart = (Spinner) layout.findViewById(R.id.homePartChoseSP);
			ArrayAdapter<HomeParts> homePartSPA = new ArrayAdapter<HomeParts>(this, android.R.layout.simple_spinner_item);
			homePart.setAdapter(homePartSPA);
			homePart.setOnItemSelectedListener(this);
			homePartSPA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			for (HomeParts hp : HomeParts.values()) {
				homePartSPA.add(hp);
			}

			actionChose = (Spinner) layout.findViewById(R.id.actionChoseSP);
			actionTypesSPA = new ArrayAdapter<ActionTypes>(this, android.R.layout.simple_spinner_item);
			actionChose.setAdapter(actionTypesSPA);
			actionTypesSPA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			timePicker = (TimePicker) layout.findViewById(R.id.timeTP);

			DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Action a = new Action((HomeParts) homePart.getSelectedItem(), (ActionTypes) actionChose.getSelectedItem(), new Date(0, 0, 0, timePicker
							.getCurrentHour(), timePicker.getCurrentMinute()));
					dataConnect.add((Plan) selectedPlanSP.getSelectedItem(), a);
					updateActionList();
				}
			};

			DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					addDialog.dismiss();
				}
			};

			builder = new AlertDialog.Builder(this);
			builder.setView(layout);
			builder.setMessage(getString(R.string.addAction));
			builder.setPositiveButton("OK", okListener);
			builder.setNegativeButton("Anuluj", cancelListener);
			addDialog = builder.create();
			addDialog.show();
			return true;
		}
		case R.id.addPlanMI: {
			AlertDialog.Builder builder;

			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.addplandialog, (ViewGroup) findViewById(R.id.layout_root));

			newPlanName = (EditText) layout.findViewById(R.id.newPlanNameET);

			DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Plan p = new Plan();
					p.setName(newPlanName.getText().toString());
					dataConnect.addPlan(p);
					updatePlans();
					updateActionList();
					updateCanChangeToActive();
				}
			};

			DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					addPlanDialog.dismiss();
				}
			};

			builder = new AlertDialog.Builder(this);
			builder.setView(layout);
			builder.setMessage(getString(R.string.addPlanMI));
			builder.setPositiveButton("Dodaj", okListener);
			builder.setNegativeButton("Anuluj", cancelListener);
			addPlanDialog = builder.create();
			addPlanDialog.show();
			return true;

		}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.schedulermenu, menu);
		return true;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		switch (parent.getId()) {
		case R.id.selectPlanSP: {
			updateActionList();
			updateCanChangeToActive();
			break;
		}
		case R.id.homePartChoseSP: {
			HomeParts selected = (HomeParts) homePart.getSelectedItem();
			actionTypesSPA.clear();
			if (selected == HomeParts.ALL_HOUSE) {
				actionTypesSPA.add(ActionTypes.HEATING_18);
				actionTypesSPA.add(ActionTypes.HEATING_20);
				actionTypesSPA.add(ActionTypes.HEATING_22);
				actionTypesSPA.add(ActionTypes.HEATING_24);
				actionTypesSPA.add(ActionTypes.HEATING_OFF);
			}
			else {
				actionTypesSPA.add(ActionTypes.TURN_ON_LIGHTS);
				actionTypesSPA.add(ActionTypes.TURN_OFF_LIGHTS);
			}
			updateActionList();
			break;
		}
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// This method will be not used, but it must be implemented
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		if (parent.getId() == R.id.planLV) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Czy na pewno chcesz usunąć").setCancelable(false).setPositiveButton("Tak", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Action a = (Action) listAdapter.getItem(selectedPos);
					dataConnect.removeActionFromPlan((Plan) selectedPlanSP.getSelectedItem(), a);
					updateActionList();
				}
			}).setNegativeButton("Nie", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.isActivePlanBT){
			Plan p = (Plan) selectedPlanSP.getSelectedItem();
			dataConnect.setActivePlan(p);
			updateCanChangeToActive();
		}
		
	}

}